<?php

namespace Api;

use Logic\CaptchaLogic;
use Logic\RedisClientLogic;

header('Access-Control-Allow-Origin: *');

class CommonController
{
    /**
     * 验证登录用户
     * @param string $token
     * @return array
     */
    public function validateUser($token = '')
    {
        $redis = RedisClientLogic::getInstance();
        $user = $redis->hGetAll('token:' . $token);
        return $user;
    }

    /**
     * 构造返回参数
     * @param int $code
     * @param string $msg
     * @param array $data
     */
    public function returnJson($code = 0, $msg = '', $data = [])
    {
        header('Content-Type:application/json; charset=utf-8');
        $res = [
            'status' => $code,
            'msg' => $msg,
            'data' => $data
        ];
        echo json_encode($res, JSON_UNESCAPED_UNICODE);
        exit;
    }

    /**
     * 生成验证码
     */
    public function captcha()
    {
        session_start();/*开启会话*/
        $type = $_GET['type'] ?? '';
        $random_code = $_GET['random_code'] ?? '';
        if ($type) {
            $captcha = new CaptchaLogic();    //实例化验证码类(可自定义参数)
            $captcha->type = $type;
            $captcha->random_code = $random_code;
            $captcha->show();    //调用输出
        }
    }
}

